<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<style>
#output tr{
	height:20pt;
	text-align:center;
}
.outputHeder{
	background: #6dfff2;
}
</style>

<script type="text/javascript" src="../js/jquery-1.10.2.js"></script>
<script type="text/javascript">

var arr = "";
$(document).ready(function() {
/* 	if('${userId}'!=null){
		alert('로그인중');
	}else{
		alert('로그아웃중');
		location.href="shopMain.do";
	} */
	function check() {
		arr = "";

		var s_c_no = $('input[name=s_c_no]:checked');

		for ( var i = 0; i < s_c_no.length; i++) {
			if (s_c_no[i].checked) {
				
				if(i > 0){
					arr += ",";	
				}
				arr += s_c_no[i].value;
			}
		}
	}
	
 	$("#btnBuy").click(function(){
 		check();
		
	$.getJSON("cartItemBuy.do",{c_no : arr},
		function(data){
		var output = '';
		var count = 0;	
		$(data).each(function(index,item){
			
			if(data.result=='ealack'){
				alert("재고수량이 부족합니다.");
			}else if(data.result=='failure'){
				alert("에러발생");
			}else{
			
				if(count == 0){
					$("#output").empty();
					var head = "<tr class=outputHeder><td>번호</td><td>제품분류</td><td>제품이름</td><td>수량</td><td>금액</td></tr>";
					$("#output").append(head);  
					count = 1;					
				}
				
				output += "<tr>";
				output += "<td><input type=checkbox name=s_c_no value="+item.c_no+">"+ item.c_no + "</td>";
				output += "<td>" + item.i_category + "</td>";
				output += "<td>" + item.i_name + "</td>";
				output += "<td>" + item.c_ea + "</td>";
				output += "<td>" + item.c_ea*item.i_price + "</td>";
				output += "</tr>";
				
				$("#output").append(output);  
				output = '';
			
			}
		 })}
		);
	});

 	$("#btnDelete").click(function(){
 		check();
 		$.getJSON("cartItemDelete.do",{c_no : arr},
 				function(data){
 				var output = '';
 				
 				$("#output").empty();
				var head = "<tr class=outputHeder><td>번호</td><td>제품분류</td><td>제품이름</td><td>수량</td><td>금액</td></tr>";
				$("#output").append(head);  
					
 				$(data).each(function(index,item){
 					
 					if(data.result=='failure'){
 						alert("에러발생");
 					}else{
 						
 						output += "<tr>";
 						output += "<td><input type=checkbox name=s_c_no value="+item.c_no+">"+ item.c_no + "</td>";
 						output += "<td>" + item.i_category + "</td>";
 						output += "<td>" + item.i_name + "</td>";
 						output += "<td>" + item.c_ea + "</td>";
 						output += "<td>" + item.c_ea*item.i_price + "</td>";
 						output += "</tr>";
 						
 						$("#output").append(output);  
 						output = '';
 					
 					}
 				 })}
 				);
 		
 	});



});

</script>

<form name="myForm" action="cartItemBuy.do" method="post" onsubmit="return check()">

<table align="center" border="0" cellpadding="0" cellspacing="1" bgcolor="#a0a0a0" width="800">

	<table id="output" align="center" width="720" border="0" cellspacing="0" cellpadding="2">
		<tr class="outputHeder">
			<td>번호</td>
			<td>제품분류</td>
			<td>제품이름</td>
			<td>수량</td>
			<td>금액</td>
		</tr>
		<c:forEach var="list" items="${cartList }">
		<tr>
			<td>
				<input type="checkbox" name="s_c_no" value="${list.c_no }" />${list.c_no }
			</td>
			<td>${list.i_category }</td>
			<td>${list.i_name }</td>
			<td>${list.c_ea	 }</td>
			<td>${list.i_price*list.c_ea }</td>
		</tr>
		</c:forEach>
	</table>
<div align="center" height="50">
	<input type="button" id="btnBuy" value="주문"/>
	<input type="button" id="btnDelete" value="삭제"/>
</div>
</table>
</form>